package com.gxa.learning.pc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.learning.core.common.ExceptionCode;
import com.gxa.learning.core.exception.BusinessException;
import com.gxa.learning.core.pojo.Feedback;
import com.gxa.learning.core.pojo.Project;
import com.gxa.learning.core.service.impl.BaseServiceImpl;
import com.gxa.learning.core.util.StringUtil;
import com.gxa.learning.pc.mapper.FeedbackMapper;
import com.gxa.learning.pc.mapper.ProjectMapper;
import com.gxa.learning.pc.service.FeedbackService;
import com.gxa.learning.pc.service.ProjectApplyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


/**
 * @version E-learning
 * @Author lonny
 * @Date 2021/12/3 9:34
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class FeedbackServiceImpl extends BaseServiceImpl<Feedback> implements FeedbackService {

    @Autowired(required = false)
    private FeedbackMapper feedbackMapper;

    @Autowired(required = false)
    private ProjectMapper projectMapper;

    @Override
    public Map<String, Object> findByDetails(String id) throws Exception {
        if (StringUtil.isEmpty(id)) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_NOT_NULL);
        }
        Map<String, Object> byDetails = feedbackMapper.findByDetails(id);
        if (byDetails.size() == 0) {
            throw new BusinessException(ExceptionCode.ID_WRONG);
        }

        return byDetails;

    }

    /**
     * 项目反馈情况
     *
     * @param id 项目id
     * @return 返回结果
     * @throws Exception 抛出
     */
    @Override
    public Map<String, Object> findFeedbackRatio(String id) throws Exception {
        if (StringUtil.isEmpty(id)) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_NOT_NULL);
        }
        Map<String, Object> feedbackRatio = feedbackMapper.findFeedbackRatio(id);
        if (feedbackRatio.size() < 1) {
            throw new BusinessException(ExceptionCode.ID_WRONG);
        }
        return feedbackRatio;
    }

    /**
     * 当前项目学员列表
     *
     * @param id   项目id
     * @param page 页码
     * @param size 页数
     * @return 返回结果
     * @throws Exception 抛出
     */
    @Override
    public PageInfo<Map<String, Object>> selectStudentList(Integer page, Integer size, String id, String userName, String company, String position) throws Exception {
        if (page == null) {
            page = 0;
        }
        if (size == null) {
            size = 10;
        }
//        判断查询的条件是否为空
        boolean userNameBlank = StringUtil.isBlankOrNull(userName);
        boolean companyBlank = StringUtil.isBlankOrNull(company);
        boolean positionBlank = StringUtil.isBlankOrNull(position);
        if (StringUtils.isEmpty(id) || StringUtils.isBlank(id)) {
            throw new BusinessException(ExceptionCode.PROJECT_FIND_WRONG);
        }
        if (userNameBlank) {
            userName = null;
        }
        if (companyBlank) {
            company = null;
        }
        if (positionBlank) {
            position = null;
        }
        PageHelper.startPage(page, size);
        List<Map<String, Object>> maps = feedbackMapper.selectStudentList(id, userName, company, position);
        if (maps.size() < 1) {
            throw new BusinessException(ExceptionCode.ID_WRONG);
        }
        return PageInfo.of(maps);
    }

    /**
     * 项目得分
     *
     * @param id 项目id
     * @return 返回结果
     * @throws Exception 抛出
     */
    @Override
    public Map<String, Object> scoreRatio(String id) throws Exception {
        if (StringUtils.isEmpty(id) || StringUtils.isBlank(id)) {
            throw new BusinessException(ExceptionCode.PROJECT_FIND_WRONG);
        }
        Project project = projectMapper.selectByPrimaryKey(id);
        if (project == null) {
            throw new BusinessException(ExceptionCode.ID_WRONG);
        }
        return feedbackMapper.scoreRatio(id);
    }

    @Autowired
    private ProjectApplyService projectApplyService;

    @Override
    public List<Map> getFeedBack(Long id) {

        return feedbackMapper.getFeedBack(id);


    }

    @Override
    public List<Map> getCourseFeedBack(Long id) {
        List<Map> courseFeedBack = feedbackMapper.getCourseFeedBack(id);
        return courseFeedBack;
    }

    /**
     * 反馈表成本目录
     *
     * @param id 项目编号
     * @return 返回结果
     * @throws Exception 抛出
     */
    @Override
    public PageInfo<Map<String, Object>> costList(Integer page, Integer size, String id) throws Exception {
        if (page == null) {
            page = 0;
        }
        if (size == null) {
            size = 10;
        }
        if (StringUtils.isEmpty(id) || StringUtils.isBlank(id)) {
            throw new BusinessException(ExceptionCode.PROJECT_FIND_WRONG);
        }
        PageHelper.startPage(page, size);
        List<Map<String, Object>> maps = feedbackMapper.costList(id);
        if (maps.size() < 1) {
            throw new BusinessException(ExceptionCode.ID_WRONG);
        }
        return PageInfo.of(maps);
    }

    /**
     * 提醒用户反馈
     *
     * @param userId    用户
     * @param projectId 项目
     */
    @Override
    public void remindFeedBack(String userId, String projectId) throws Exception {
        if (StringUtil.isBlankOrNull(userId) || StringUtil.isBlankOrNull(projectId)) {
            throw new BusinessException(ExceptionCode.NO_ID_ERROR);
        }
        Feedback byUserAndProject = feedbackMapper.findByUserAndProject(userId, projectId);
        if (byUserAndProject == null) {
            throw new BusinessException(ExceptionCode.NO_ID_ERROR);
        }
        if (byUserAndProject.getState() == 1) {
            throw new BusinessException(ExceptionCode.REMIND_REPEAT);
        }
        deleteById(byUserAndProject.getId());
        Long userId1 = Long.valueOf(userId);
        Long projectId1 = Long.valueOf(projectId);
        projectApplyService.insetFeed(userId1, projectId1);


    }
}
